package jetzhu;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class HashX {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap() {{
            put("2", "b");
            put("3", "c");
            put("1", "a");
            put("4", "d");
            put("5", null);
        }};

        //根据key排序输出
        map.keySet().stream().sorted().forEach(
                k -> System.out.println(k + ", " + map.get(k))
        );

        //根据value排序输出
        map.entrySet().stream().sorted((o1, o2) -> {
            if (Objects.isNull(o1.getValue())) {
                return -1;
            }
            return o1.getValue().compareTo(o2.getValue());
        }).forEach(e -> System.out.println(e.getKey() + ", " + e.getValue()));
    }
}
